var app = getApp();
var request = require('../../utils/requestapi.js');
var api = require('../../api.js');
var utils = require('../../utils/util.js');
var tabbarTemplate = require("../tab-bar/tab-bar.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: [],
    imgUrls: [
      '../../images/banner-img1.jpg',
      '../../images/banner-img2.jpg',
      '../../images/banner-img3.jpg',
    ],
    indicatorDots: true,
    autoplay1: true,
    interval1: 5000,
    duration1: 500,
    main_menu: [],
    live_list: [],
    video_list: [],
    pintuan: [], //拼团列表
    free: [], //免费课程列表
    index_plate: [],
    rec_goods: [],
    live_auth: false,
  },



  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function(options) {

    if (options) {
      wx.setStorageSync("tmp_options", options)
    }

    tabbarTemplate.tabbar("tabBar", 0, this, 'home')
    this.getIndexData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/index/index?pid=' + user.user_id,
    }
  },

  //获取首页列表
  getIndexData: function(e) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })

    getApp().request({
      url: api.index.banner,
      method: "GET",
      success: function(response) {
        if (response.errcode == 0) {
          that.setData({
            banner: response.data.banner,
            main_menu: response.data.menu,
            live_list: response.data.live_index,
            index_plate: response.data.index_plate,
            rec_goods: response.data.rec_goods,
            live_auth: response.data.live_auth,
            live_title: response.data.live_title ? response.data.live_title : '',
            rec_title: response.data.rec_title ? response.data.rec_title : '',
            rec_intro: response.data.rec_intro ? response.data.rec_intro : '',
          });
        } else {
          wx.showToast({
            title: response.errmsg,
            icon: 'none'
          })
        }

      },
      complete: function() {
        wx.hideLoading();
      }
    });

  },


  //tabbar跳转
  tabBarRedirect: function(e) {
    wx.redirectTo({
      url: e.currentTarget.dataset.url,
    })
  }
})